package com.lanyou.esb.cook.proxy.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.lanyou.esb.cook.proxy.dao.impl.BaseDao;
import com.lanyou.esb.cook.proxy.entity.PfThroughput;
import com.lanyou.esb.cook.proxy.redis.CommonUtils;
import com.lanyou.esb.cook.proxy.redis.proxy.impl.PfThroughputJedisProxy;
import com.lanyou.esb.cook.proxy.redis.proxy.impl.PfThroughputKeysJedisProxy;
import com.lanyou.esb.cook.proxy.redis.vo.Group;
import com.lanyou.esb.cook.proxy.redis.vo.Group.Entry;
import com.lanyou.esb.cook.proxy.redis.vo.RedisActionType;
import com.lanyou.esb.cook.proxy.service.GroupService;
import com.lanyou.esb.cook.proxy.service.PfThroughputService;
import com.lanyou.esb.cook.proxy.service.RedisBatchService;
import com.lanyou.esb.cook.proxy.service.Service;
import com.lanyou.esb.cook.proxy.service.push.Push;

/**
 * 平台吞吐量服务
 * 
 * @author Davey.wu
 */
public class PfThroughputServiceImpl implements PfThroughputService,
		GroupService<PfThroughput>, Service<PfThroughput> {
	/** 数据库操作接口 **/
	private BaseDao<PfThroughput> dao;
	/** 平台吞吐量REDIS操作代理 **/
	private PfThroughputJedisProxy pfThroughputJedisProxy;
	/** 平台吞吐量键值,REDIS处理器 **/
	private PfThroughputKeysJedisProxy pfThroughputKeysJedisProxy;
	/** 消息推送接口 **/
	private Push<PfThroughput> push;
	/** Redis批处理接口 **/
	private RedisBatchService redisBatchService;

	@Override
	public Group getGroup(PfThroughput pfThroughput) {
		if (pfThroughput == null) {
			return null;
		}
		// 统计时间不能为空
		if (pfThroughput.getStatisticsDate() == null) {
			return null;
		}
		// 吞吐量为0，返回
		if (pfThroughput.getThroughputQty() <= 0) {
			return null;
		}
		String json = "";
		try {
			// 将对象转换成JSON字符串
			json = CommonUtils.toJson(pfThroughput);
		} catch (Exception e) {
			return null;
		}
		String key = pfThroughputJedisProxy.getKeyFromObject(pfThroughput);
		Group group = new Group();
		// string类型条件集合
		List<Entry> strs = new ArrayList<>(2);
		// set类型条件集合
		List<Entry> sets = new ArrayList<>(1);
		group.setStrs(strs);
		group.setSets(sets);

		// 平台吞吐量对象
		Entry entry = new Entry();
		entry.setKey(key);
		entry.setValue(json);
		entry.setTimeOut(pfThroughputJedisProxy.getTimeOut());
		entry.setType(RedisActionType.SET);
		strs.add(entry);

		// 平台吞吐量增加
		entry = new Entry();
		entry.setKey(PfThroughputJedisProxy.getQytKey(key));
		entry.setValue(pfThroughput.getThroughputQty() + "");
		entry.setTimeOut(pfThroughputJedisProxy.getTimeOut());
		entry.setType(RedisActionType.INCR_BY_FLOAT);
		strs.add(entry);

		// 添加更新键
		entry = new Entry();
		entry.setKey(pfThroughputKeysJedisProxy.getKey());
		entry.setValue(key);
		sets.add(entry);

		return group;
	}

	@Override
	public int pushToRedis(PfThroughput pfThroughput) {
		Group group = getGroup(pfThroughput);
		if (group == null) {
			return 0;
		}
		int result = 0;
		try {
			// 批量将消息推送至redis
			redisBatchService.batchPush(group);
		} catch (Exception e) {
			push.putToCollection4Redis(pfThroughput);
		}
		return result;
	}

	@Override
	public int pushToRedis(List<PfThroughput> pfThroughputs) {
		if (pfThroughputs == null) {
			return 0;
		}
		int result = 0;
		for (PfThroughput pfThroughput : pfThroughputs) {
			result += pushToRedis(pfThroughput);
		}
		return result;
	}

	@Override
	public PfThroughput find(PfThroughput pfThroughput) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void save(final List<PfThroughput> list) throws Exception {
		if (list == null || list.isEmpty()) {
			return;
		}

		try {
			TransactionTemplate tt = new TransactionTemplate(
					this.dao.getTransactionManager());
			tt.execute(new TransactionCallback() {
				public Object doInTransaction(TransactionStatus status) {
					return PfThroughputServiceImpl.this.dao.saveAll(list);
				}
			});
		} catch (Exception e) {
			// 保留失败，将记录添加到推送队列中
			push.putToCollection4DB(list);
		}
	}

	@Override
	public void update(List<PfThroughput> list) throws Exception {
		// 目前不存在更新业务，所以不提供实现
	}

	public PfThroughputJedisProxy getPfThroughputJedisProxy() {
		return pfThroughputJedisProxy;
	}

	public void setPfThroughputJedisProxy(
			PfThroughputJedisProxy pfThroughputJedisProxy) {
		this.pfThroughputJedisProxy = pfThroughputJedisProxy;
	}

	public PfThroughputKeysJedisProxy getPfThroughputKeysJedisProxy() {
		return pfThroughputKeysJedisProxy;
	}

	public void setPfThroughputKeysJedisProxy(
			PfThroughputKeysJedisProxy pfThroughputKeysJedisProxy) {
		this.pfThroughputKeysJedisProxy = pfThroughputKeysJedisProxy;
	}

	public BaseDao<PfThroughput> getDao() {
		return dao;
	}

	public void setDao(BaseDao<PfThroughput> dao) {
		this.dao = dao;
	}

	public Push<PfThroughput> getPush() {
		return push;
	}

	public void setPush(Push<PfThroughput> push) {
		this.push = push;
	}

	public RedisBatchService getRedisBatchService() {
		return redisBatchService;
	}

	public void setRedisBatchService(RedisBatchService redisBatchService) {
		this.redisBatchService = redisBatchService;
	}

}
